package com.hellobike.haxing.modules.supplier.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.supplier.entity.TSupplier;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.supplier.service.ITSupplierService;
import com.hellobike.haxing.modules.supplier.util.BeanUtils;
import com.hellobike.haxing.modules.supplier.util.Constant;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: t_supplier
 * @Author: haxing-boot
 * @Date: 2020-01-09
 * @Version: V1.0
 */
@RestController
@RequestMapping("/supplier/tSupplier")
@Slf4j
public class TSupplierController extends BaseController<TSupplier, ITSupplierService> {
	@Autowired
	private ITSupplierService tSupplierService;
	@Autowired
	private ITSupplierInfoService tSupplierInfoService;


	@GetMapping("/list-all")
	public Result<List<TSupplier>> listAll() {
		Result<List<TSupplier>> result = new Result<>();
		result.setResult(tSupplierService.list());
		return result;
	}

	/**
	 * 分页列表查询
	 *
	 * @param tSupplier
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TSupplier tSupplier,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TSupplier> queryWrapper = QueryGenerator.initQueryWrapper(tSupplier, req.getParameterMap());
		Page<TSupplier> page = new Page<TSupplier>(pageNo, pageSize);
		IPage<TSupplier> pageList = tSupplierService.page(page, queryWrapper);
		if (pageList != null && ListUtil.isNotEmpty(pageList.getRecords())) {
			pageList.getRecords().forEach(
					item -> {
						if (item.getIsNew().equals(Constant.SUPPLIER_IS_NEW)) {
							item.setSupplierName("(新)" + item.getSupplierName());
						}
					}
			);
		}
		return Result.ok(pageList);
	}

	/**
	 * 添加
	 *
	 * @param tSupplier
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TSupplier tSupplier) {
		return tSupplierService.saveSupplier(tSupplier);
	}

	/**
	 * 编辑
	 *
	 * @param tSupplier
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TSupplier tSupplier) {
		return tSupplierService.updateSupplier(tSupplier);
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		tSupplierService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.tSupplierService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
		TSupplier tSupplier = tSupplierService.getById(id);
		if (tSupplier == null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(tSupplier);
	}

	/**
	 * 通过id生成供应商信息
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/genSupplierInfoById")
	public Result<?> genSupplierInfoById(@RequestParam(name = "id", required = true) Long id) {
		try {
			TSupplier tSupplier = tSupplierService.getById(id);
			if (tSupplier == null) {
				return Result.error("未找到对应数据");
			}
			TSupplierInfo tSupplierInfo = new TSupplierInfo();
			QueryWrapper<TSupplierInfo> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("supplier_name", tSupplier.getSupplierName());
			Integer count = tSupplierInfoService.count(queryWrapper);
			if (count < 1) {
				BeanUtils.copyProperties(tSupplier, tSupplierInfo);
				tSupplierInfo.setId(null);
				tSupplierInfo.setSupplyCategory("[]");
				// 暂时直接审核通过
				tSupplierInfo.setState(Constant.INFO_IS_PASS);
				tSupplierInfoService.save(tSupplierInfo);

				TSupplier updateDo = new TSupplier();
				updateDo.setId(tSupplier.getId());
				updateDo.setState(Constant.IS_INPUT);
				tSupplierService.updateById(updateDo);
				return Result.ok("成功生成供应商信息!");
			} else {
				return Result.error("供应商名称已存在!");
			}
		} catch (Exception e) {
			return Result.error("生成供应商信息失败!");
		}
	}


	/**
	 * 导出excel
	 *
	 * @param request
	 * @param tSupplier
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, TSupplier tSupplier) {
		return super.exportXls(request, tSupplier, TSupplier.class, "t_supplier");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, TSupplier.class);
	}

}
